/**
 * Created by duwei on 2019/3/25.
 */

var ZiPai_ChiBiWindow = Window.extend({
    _ccsFileName: res.ZiPai_ChiBiWindow_json,
    _blockLayerOpacity:0,
    _blockBack:false,
    _cardSize: 45,
    _paiMap: {},
    _selectCardMap:{},
    initWindow: function (chiArray, showNo, room) {
        var self = this;
        cc.log(chiArray);
        self.showNo = showNo;
        self.room = room;
        self.chibiNode = self._nodeList['root.chibiNode0'];
        self._paiMap = {};
        self._selectCardMap = {};
        self.setChiBiNode(0, 'chi', chiArray);

        function checkDeep() {
            var deep = 1;
            for (var i = 0; i < chiArray.length; i++){
                if (typeof(chiArray[i]) == "number"){
                    continue;
                }
                else {
                    for (var j = 0; j < chiArray[i].length; j++){
                        if (typeof(chiArray[i][j]) == "number"){
                            continue;
                        }
                        else {
                            if (deep < 2) {
                                deep = 2;
                            }
                            for (var m = 0; m < chiArray[i][j].length; m++){
                                if (typeof(chiArray[i][j][m]) == "number"){
                                    continue;
                                }
                                else {
                                    for (var n = 0; n < chiArray[i][j][m].length; n++){
                                        if (typeof(chiArray[i][j][m][n]) == "number"){
                                            continue;
                                        }
                                        else {
                                            deep = 3;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return deep;
        }


        if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) {
            var offsetXVal = checkDeep() == 3 ? -300 : 0;
            self.chibiNode.setPositionX(self.room.nAction.getChildByName("bt2").getPositionX() +
                self.room.nAction.getPositionX() -
                (self.room.nAction.getParent().getContentSize().width - self._nodeList['root'].getContentSize().width) / 2 + offsetXVal);
        }
    },
    setChiBiNode: function (count, type, data) {
        var jiange = 80;
        var self = this;
        var root = self._nodeList['root'];
        var node = root.getChildByName('chibiNode' + count);
        if (!node) {
            node = duplicateNode(self.chibiNode);
            node.setName('chibiNode' + count);
            root.addChild(node);
        }
        if(!data){
            node.setVisible(false);
            return
        }
        cc.log(data);
        node.setVisible(true);
        var topBg = node.getChildByName('topBg');
        topBg.getChildByName('bipai').setVisible(type != 'chi');
        topBg.getChildByName('chipai').setVisible(type == 'chi');
        var bg = node.getChildByName('bg');
        var pai0 = bg.getChildByName('pai0');
        var posX = pai0.getPositionX();
        self._paiMap[count] = [];
        for (var i = 0; i < data.length; i++) {
            var pai = bg.getChildByName('pai' + i);
            if (!pai) {
                pai = duplicateNode(pai0);
                pai.setName('pai' + i);
                bg.addChild(pai);
            }
            pai.setVisible(true);
            self._paiMap[count].push(pai);
            pai.setPositionX(posX + i * jiange);
            pai.getChildByName('zhuanzhong').setVisible(false);
            var paiPos = pai.getChildByName('paiPos');
            var cardData = data[i];
            var cards = [];
            for (var k = 0; k < 3; k++) {
                var cardNo = cardData[k];
                var card = new Card();
                card.setData(cardNo, null, 2);
                card.setScale(self._cardSize / 42);
                card.setAnchorPoint(0, 0);
                card.setPosition(0, k * self._cardSize);
                paiPos.addChild(card);
                cards.push(cardNo);
            }
            //排序  吃的牌放在第一位
            for (var j = 0; j < data.length; j++) {
                if (self.showNo == cards[j]) {
                    var tmp = data[0];
                    data[0] = data[i];
                    data[i] = tmp;
                    break;
                }
            }
            paiPos.data = {
                index: i,
                count: count,
                cards:cards,
                biArr: cardData[3]
            };
            TouchUtil.setOnclickListener(paiPos, self.touchChiBiNode.bind(this));
        }
        for (; i < bg.getChildrenCount(); i++) {
            var pai = bg.getChildByName('pai' + i);
            if(pai){
                pai.setVisible(false);
            }

        }
        if (data.length > 0) {
            bg.setContentSize(cc.size(posX * 2 + (data.length - 1) * jiange, bg.getContentSize().height));
        }

        if (count > 0) {
            var tmpBg = self._paiMap[count - 1][0].getParent();
            var tmpNode = tmpBg.getParent();
            cc.log(tmpNode.getPositionX());
            cc.log(tmpBg.getContentSize().width / 2);
            if (gameData.mapId == app.map_id.QYLDS ||gameData.mapId == app.map_id.YZCHZ) {
                node.setPositionX(tmpNode.getPositionX() + tmpBg.getContentSize().width / 2 + bg.getContentSize().width / 2 + 10);
            }else{
                node.setPositionX(tmpNode.getPositionX() - tmpBg.getContentSize().width / 2 - bg.getContentSize().width / 2 - 10);
            }
        }
    },
    touchChiBiNode: function (node) {
        var self = this;
        var paiList = self._paiMap[node.data.count] || [];
        for (var i = 0; i < paiList.length; i++) {
            var pai = paiList[i];
            pai.getChildByName('zhuanzhong').setVisible(node.data.index == i);
            if(node.data.index == i){
                cc.log('cards',node.data.cards);
                self._selectCardMap[node.data.count] = node.data.cards;
                self.setChiBiNode(node.data.count + 1, 'bi', node.data.biArr);
                if(!node.data.biArr){
                    var cards= [];
                    for (var j = 0; j < node.data.count+1; j++) {
                        cc.log(self._selectCardMap[j]);
                        cards = cards.concat(self._selectCardMap[j]);
                    }
                    cc.log(cards);
                    self.room.userAct(CHI, this.showNo, false, cards);
                    return;
                }
            }
        }
    },
});